package cn.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.dao.LoginDao;
import cn.pojo.Login;
/**
 * 实现类表达式
 * @author 莫文聪
 *
 */
public class LoginDaoImpl implements LoginDao {
	@Override
	public Login getUserByEmailAndPassword(Connection connection, String email, String password) throws SQLException {
		//sql语句
		String sql = "select * from user where email=? and password=?";
		//获取PreparedStatement对象（预处理）
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, email);
		ps.setString(2, password);
		//执行查询
		ResultSet rs = ps.executeQuery();
		Login userData = null;
		//如果有数据
		if(rs.next()) {
			userData = new Login();//實例化
			String email_ = rs.getString("email");
			String password_ = rs.getString("password");
			String nickname = rs.getString("nickname");
			Integer role = rs.getInt("role");
			Integer status = rs.getInt("status");
			userData.setEmail(email_);
			userData.setPassword(password_);
			userData.setNickname(nickname);
			userData.setRole(role);
			userData.setStatus(status);
		}
		return userData;
	}
}
